package ssi.staaar;

import java.util.ArrayList;
import ssi.peeno.model.User;
import ssi.staaar.control.StaaarControl;
import ssi.staar.R;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TabHost.OnTabChangeListener;

public class ManageFriendsActivity extends TabActivity {
	private StaaarControl stControl;
	private ArrayAdapter<User> users;
	private ArrayList<User> displayed = new ArrayList<User>();
	public static ArrayList<User> accepted;
	private int seeWhat;
	private int currentList;
	
	private void acceptFriendShip(User item) {
		Log.d("[MANAGEFRIENDS]", "AcceptFriendShip: " + item.getUsername());
		stControl.acceptFriend(item.getUsername());
		users.remove(item);
		ListView listView = (ListView)findViewById(currentList);
		listView.refreshDrawableState();
	}

	public void goToSearchFriendsActivity(View view) {
		Intent iSearch = new Intent();
		iSearch.setClass(getApplicationContext(), SearchFriendsActivity.class);
		startActivity(iSearch);
	}

	private void initialize() {
		stControl = Staaar.stControl;
	}

	public void loadFriends(View view) {
		seeWhat = 0;
		accepted = stControl.requestFriendList(seeWhat);
		stControl.checkKeys(accepted);
		users = new ArrayAdapter<User>(this, R.layout.friend_list_item, accepted);
		ListView listView = (ListView)findViewById(R.id.friendsList);
		listView.setAdapter(users);
		registerForContextMenu(listView);
		stControl.checkNotification();
	}
	
	public void loadRequestToMe(View view) {
		seeWhat = 1;
		displayed = stControl.requestFriendList(seeWhat);
		users = new ArrayAdapter<User>(this, R.layout.friend_list_item, displayed);
		ListView listView = (ListView)findViewById(R.id.requestToMeList);
		listView.setAdapter(users);
		registerForContextMenu(listView);
	}
	
	public void loadRequestFromMe(View view) {
		seeWhat = 2;
		displayed = stControl.requestFriendList(seeWhat);
		users = new ArrayAdapter<User>(this, R.layout.friend_list_item, displayed);
		ListView listView = (ListView)findViewById(R.id.requestFromMeList);
		listView.setAdapter(users);
		registerForContextMenu(listView);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
			case R.id.acceptFriendItem:
				acceptFriendShip(users.getItem(info.position));
				return true;
			case R.id.removeFriendItem:
				removeFriendShip(users.getItem(info.position));
				return true;
			case R.id.refuseFriendItem:
				refuseFriendShipRequest(users.getItem(info.position));
				return true;
			case R.id.sendPrivateMessage:
				sendPrivateMessage(users.getItem(info.position));
				return true;
			default:
				return super.onContextItemSelected(item);
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.managefriends);
	    TabHost mTabHost = (TabHost)findViewById(android.R.id.tabhost);
	    mTabHost.addTab(mTabHost.newTabSpec("friendsTab").setIndicator("Friends").setContent(R.id.friendsList));
	    mTabHost.addTab(mTabHost.newTabSpec("requestToMeTab").setIndicator("Received").setContent(R.id.requestToMeList));
	    mTabHost.addTab(mTabHost.newTabSpec("requestFromMeTab").setIndicator("Sent").setContent(R.id.requestFromMeList));
	    mTabHost.setCurrentTab(0);
	    initialize();
	    loadFriends(null);
	    currentList = R.id.friendsList;
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				System.out.println(tabId);	
				if (tabId.equalsIgnoreCase("friendsTab")) {
					loadFriends(null);
					currentList = R.id.friendsList;
				}
				if (tabId.equalsIgnoreCase("requestToMeTab")) {
					loadRequestToMe(null);
					currentList = R.id.requestToMeList;
				}
				if (tabId.equalsIgnoreCase("requestFromMeTab")) {
					loadRequestFromMe(null);
					currentList = R.id.requestFromMeList;
				}
				
			}
		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		if (seeWhat == 0)
			inflater.inflate(R.menu.managefriendslistmenu0, menu);
		else if (seeWhat == 1)
			inflater.inflate(R.menu.managefriendslistmenu1, menu);
	}
	
	private void refuseFriendShipRequest(User item) {
		Log.d("[MANAGEFRIENDS]", "RefuseFriendShip: " + item.getUsername());
		stControl.refuseFriend(item.getUsername());
		users.remove(item);
		ListView listView = (ListView)findViewById(currentList);
		listView.refreshDrawableState();
	}

	private void removeFriendShip(User item) {
		Log.d("[MANAGEFRIENDS]", "RemoveFriendShip: " + item.getUsername());
		stControl.removeFriend(item.getUsername());
		users.remove(item);
		ListView listView = (ListView)findViewById(currentList);
		listView.refreshDrawableState();
	}

	private void sendPrivateMessage(User item) {
		Log.d("[MANAGEFRIENDS]", "Trying to send a private message to: " + item.getUsername());
		Intent iAddPrivateMessage = new Intent();
		iAddPrivateMessage.putExtra("Receiver", item.getUsername());
		iAddPrivateMessage.setClass(getApplicationContext(), AddMessageActivity.class);
		startActivity(iAddPrivateMessage);
	}
}
